import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author Andrew Yang
 * @ClassName Producer3Publish
 * @Description
 * @Package PACKAGE_NAME
 * @create 2020-02-05 16:42
 */
public class Producer3Publish {
    private static final String QUEUE_INFORM_EMAIL="queue_inform_email";
    private static final String QUEUE_INFORM_CMS = "queue_inform_cms";
    private static final String EXCHANGE_ROUTING_INFORM = "exchange_routing_inform";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_ROUTING_INFORM, BuiltinExchangeType.DIRECT);
        channel.queueDeclare(QUEUE_INFORM_CMS, true, false, false, null);
        channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);

        channel.queueBind(QUEUE_INFORM_CMS, EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_CMS);
        channel.queueBind(QUEUE_INFORM_EMAIL, EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_EMAIL);
        for (int i = 0; i < 10; i++) {
            String message = "email inform to user"+i;
            channel.basicPublish(EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_EMAIL, null, message.getBytes());
            System.out.println("Send Message is:'" + message + "'");
        }
        for (int i = 0; i < 10; i++) {
            String message = "sms inform to user" + i;
            channel.basicPublish(EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_CMS, null, message.getBytes());
            System.out.println("Send Message is:'" + message + "'");
        }
    }
}
